<?php
if (!defined('IN_MEDIA')) die("Hacking attempt");
if(!empty($keyword)) {
	$kw 									= m_htmlchars(strtolower(utf8_to_ascii(urldecode($keyword))));
		################
		### SEARCH SONG ###
		################
		if($type == 'song') {
			$num 							= $mysql->query("SELECT * FROM ".$tb_prefix."data WHERE m_title_ascii LIKE '%".$kw."%'");
			$total 							= $mysql->num_rows($num);
			if (!$pg) $pg = 1;
			$m_per_page 					= m_get_config("media_per_page");
			$sql 							= "SELECT * FROM ".$tb_prefix."data WHERE m_title_ascii LIKE '%".$kw."%' ORDER BY m_title_ascii ASC LIMIT ".(($pg-1)*$m_per_page).",".$m_per_page;
			$query 							= $mysql->query($sql);
				if($mysql->num_rows($query)) {
					while($row = $mysql->fetch_array($query)) {
						switch ($row['m_type']) {
							case 1 : $media_type = 'music'; break;
							case 2 : $media_type = 'flash'; break;
							case 3 : $media_type = 'movie'; break;
						}
						$song_TYPE 			 = "<img src='$tpl_link/img/media/type/$media_type.gif'>";
						$song_LYRIC 	 	 = ($row_song['m_lyric'])?"<img src='$tpl_link/img/media/ok.gif'>":'';
						$song_ID 			 = $row['m_id'];
						$song_TITLE 		 = $row['m_title'];
						
						$singer_ID			 =  $row['m_singer'];
						$singer_NAME    	 =  m_get_data('SINGER',$singer_ID);
						
						$song_VIEWED		 = $row['m_viewed'];
						$song_DOWNLOADED 	 = $row['m_downloaded'];
						$cat_ID =  $row['m_cat'];
						$cat_NAME =  m_get_data('CAT',$cat_ID);
						$cat_URL    = url_link($cat_ID,$cat_NAME,'Cat');
						$song_URL			 = url_link($song_ID,$song_TITLE,'nhac-san');
						$singer_URL 		 = url_link($singer_ID,$singer_NAME,'Singer');
						$song_TITLE1 = $song_TITLE;
						$link		 		 = $mainURL."/index.php?go=search&keyword=$kw&type=song&pg=";
						$view_pages  		 = m_viewpages($total,$m_per_page,$pg,$link);
						$user_ID = $row['m_poster'];
						$user_NAME =  m_get_data('USER',$user_ID);
						if($user_NAME == '')
						{
							$user_NAME = 'DJ Other';
						}
						$addp = 'addToPlaylist('.$song_ID.');return false;';	
						if (!$isLoggedIn){
							$addp = 'alert("Yêu cầu đăng nhập")';
						}
						$m_rate = $row['m_rating'];
						$m_rate_total = $row['m_rating_total'];
						if ($m_rate =='0') $current_star = 0;

						else $rater_rating = $m_rate / $m_rate_total;
						if ($rater_rating <= 0  ) $star = 0;
						if ($rater_rating >= 0.5) $star = 1;
						if ($rater_rating >= 1) $star = 2;
						if ($rater_rating >= 1.5) $star = 3;
						if ($rater_rating >= 2) $star = 4;
						if ($rater_rating >= 2.5) $star = 5;
						if ($rater_rating >= 3) $star = 6;
						if ($rater_rating >= 3.5) $star = 7;
						if ($rater_rating >= 4) $star = 8;
						if ($rater_rating >= 4.5) $star = 9;
						if ($rater_rating >= 5) $star = 10;
						$song_HTML .= "<li><p class='right'><span class='rate s_".$star."'><span><span><span><span><span></span></span></span></span></span></span><span class='add_pl'><a href='#' onclick='$addp' title='Add to Playlist'></a></span></p><p class='title'><a href='$song_URL' title='$song_TITLE'>$song_TITLE1</a></p><p class='user'>Đăng bởi: <a href='$user_URL'>$user_NAME</a> | <a href='$cat_URL'>$cat_NAME</a> | Lượt nghe: $song_VIEWED</p></li>";
					}
				}
				if(!$mysql->num_rows($query)) {
					$song_HTML 				 = "";
					$song_search_advance	= '<div id="page">
    
    <form id="searchForm" method="post">
		<fieldset>
        
           	<input id="s" type="text" value="'.$kw.'" />
            
            <input type="submit" value="Submit" id="submitButton" />
            
            <div id="searchInContainer">
                <input type="radio" name="check" value="site" id="searchSite" checked />
                <label for="searchSite" id="siteNameLabel">Search</label>
                
                <input type="radio" name="check" value="web" id="searchWeb" />
                <label for="searchWeb">Search The Web</label>
			</div>
                        
            <ul class="icons">
                <li class="web" title="Web Search" data-searchType="web">Web</li>
                <li class="images" title="Image Search" data-searchType="images">Images</li>
                <li class="news" title="News Search" data-searchType="news">News</li>
                <li class="videos" title="Video Search" data-searchType="video">Videos</li>
            </ul>
            
        </fieldset>
    </form>

    <div id="resultsDiv"></div>
    
</div>

<!-- It would be great if you leave the link back to the tutorial. Thanks! -->
    
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script src="js/script.js"></script>';
				}
		include("templates/".$_SESSION['current_tpl']."/search_song.html");
	}
		#################
		### SEARCH SINGER ###
		#################
		if($type == 'singer') {
			$num 							 = $mysql->query("SELECT * FROM ".$tb_prefix."singer WHERE singer_name_ascii LIKE '%".$kw."%'");
			$total 							 = $mysql->num_rows($num);
			if (!$pg) $pg = 1;
			$m_per_page 					 = m_get_config("media_per_page");
			$sql 							 = "SELECT * FROM ".$tb_prefix."singer WHERE singer_name_ascii LIKE '%".$kw."%' ORDER BY singer_name_ascii ASC LIMIT ".(($pg-1)*$m_per_page).",".$m_per_page;
			$query 							 = $mysql->query($sql);
				if($mysql->num_rows($query)) {
					while($row = $mysql->fetch_array($query)) {
						$singer_ID			 = $row['singer_id'];
						$singer_NAME 		 = $row['singer_name'];
						$singer_IMG 		 = m_get_img('Album',$row['singer_img']);
						
						$singer_URL 		 = url_link($singer_ID,$singer_NAME,'Singer');
						
						$link		  		 = $mainURL."/index.php?go=search&keyword=$kw&type=singer&pg=";
						$view_pages  		 = m_viewpages($total,$m_per_page,$pg,$link);
						
						$singer_HTML 		.= "<tr><td width=10%><img src=$singer_IMG></td><td width=90%><a href=$singer_URL><b>$singer_NAME</b></a></td></tr>";
					}
				}
				if(!$mysql->num_rows($query)) {
					$singer_HTML 			 = "<tr align=center><td align=center width=100%>Không tìm thấy kết quả với từ khóa <b>".$kw."</b></td></tr>";
				}
		include("templates/".$_SESSION['current_tpl']."/search_singer.html");
	}
	################
	### SEARCH ALBUM ###
	################
	if($type == 'album') {
			$num 							 = $mysql->query("SELECT * FROM ".$tb_prefix."album WHERE album_name_ascii LIKE '%".$kw."%'");
			$total 							 = $mysql->num_rows($num);
			if (!$pg) $pg = 1;
			$m_per_page 					 = m_get_config("media_per_page");
			$sql 							 = "SELECT * FROM ".$tb_prefix."album WHERE album_name_ascii LIKE '%".$kw."%' ORDER BY album_name_ascii ASC LIMIT ".(($pg-1)*$m_per_page).",".$m_per_page;
			$query 							 = $mysql->query($sql);
				if($mysql->num_rows($query)) {
					while($row = $mysql->fetch_array($query)) {
						$album_ID			 = $row['album_id'];
						$album_NAME 		 = $row['album_name'];
						$album_IMG			 = m_get_img('Album',$row['album_img']);
						
						$singer_ID			 = $row['album_singer'];
						$singer_NAME 		 = m_get_data('SINGER',$singer_ID);
						
						$singer_URL 		 = url_link($singer_ID,$singer_NAME,'Singer');
						$album_URL			 = url_link($album_ID,$album_NAME,'Album');
						
						$link		  		 = $mainURL."/index.php?go=search&keyword=$kw&type=album&pg=";
						$view_pages   		 = m_viewpages($total,$m_per_page,$pg,$link);
						
						$album_HTML	   		.= "<tr><td width=10%><img src=$album_IMG></td><td width=90%><a href=$album_URL><b>$album_NAME</b></a><br> &#187; <a href=$singer_URL>$singer_NAME</a></td></tr>";
					}
				}
				if(!$mysql->num_rows($query)) {
					$album_HTML 			 = "<tr align=center><td align=center width=100%>Không tìm thấy kết quả với từ khóa <b>".$kw."</b></td></tr>";
				}
		include("templates/".$_SESSION['current_tpl']."/search_album.html");
	}
					
}
?>